<?php
//includes/mod_function.inc.php
  include_once('eac_curl.class.php');
  include_once('config.php');
  include_once('foxydb.php');

  $foxydb = new Foxydb();

	/**
	* check whether the website has a verfied image,used for checking whether the user is webmaster
	*/
	function hasVerifyImage($puri){
		global 	$VERIFY_MOTHER_IMAGE_NAME ,$_DEBUG;

		$pverify_site_image_path = $puri.$VERIFY_MOTHER_IMAGE_NAME;
		$http = new cURL();
		$result = $http->get($pverify_site_image_path);
		if ($http->success){
			$info = $http->getInfo();
			if($info['http_code'] == 200 && $info['url'] == $pverify_site_image_path){ //ok && prevent evil redirect 
				return true;
			}
		}
		if ($_DEBUG){
			echo "<pre>";
			print_r($http->getHeaders());
			print_r($http->getInfo());
			print_r($http->getOptions());
			echo "</pre>";
		}
		return false;
	}


function ip_address_to_number($IPaddress)
{
    if ($IPaddress == "") {
        return 0;
    } else { 
        $ips = split ("\.", "$IPaddress");
        return ($ips[3] + $ips[2] * 256 + $ips[1] * 256 * 256 + $ips[0] * 256 * 256 * 256);
    }
}


function getHtmlTitle($purl){
	if ($handle = @fopen($purl, "r")) {
		$content = "";
		while (!feof($handle)) {
			$part = fread($handle, 1024);
			$content .= $part;
			if (eregi("</head>", $part)) break;
		}
		fclose($handle);
		$lines = preg_split("/\r?\n|\r/", $content); // turn the content in rows
		$close_tag = ($xhtml) ? " />" : ">"; // new in ver. 1.01
		foreach ($lines as $val) {
			if (eregi("<title>(.*)</title>", $val, $title)) {
				$page_title = $title[1];
				return $page_title;
			}
		}
	}

}

Class FoxySite{
 
  //
  //Site -1----------* Site2Category *----------1- Category
  //
  function getRelatedSitesByUriPrePath($pUriPrePath,$num=60,$pnum=10)
	{
		global $foxydb,$FXF_TOPPEST_COMMON_SITE;


		$foxydb->open();

		$site = $foxydb->findSiteByUri($pUriPrePath);

		$primaryPath = $foxydb->findCategoryPrimaryPath($site["primarycatsid"]);
		$result["primarypath"] = $primaryPath;

		$primaryAreaPath = $foxydb->findCategoryPrimaryPath($site["primaryareacatsid"]);
		$result["primaryareapath"] = $primaryAreaPath;
		
		$result["site"] = array($site);

		if($primaryPath){
			foreach ($primaryPath as $cat){

				if($cat["sid"] == $site["primarycatsid"]){
					$sites = $foxydb->selectSitesByCatSid($cat["sid"],$num);
				}else{
					$count = $foxydb->findSiteCountByCatSid($cat["sid"]);
					$start = rand(0, $count);
					$sites = $foxydb->selectSitesByCatSid($cat["sid"],$pnum,$start);
				}

				if($result["site"] ){
					if($sites){
						$result["site"] = array_merge($result["site"],$sites);
					}
				}else{
					$result["site"] = $sites;
				}
			}
		}
		if($result["site"]){
			foreach ($result["site"] as $site){
				$s2cs = $foxydb->selectSite2CategoriesBySiteSid($site["sid"]);
				if($result["s2c"] ){
					if($s2cs){
						$result["s2c"] = array_merge($result["s2c"],$s2cs);
					}
				}else{
					$result["s2c"] = $s2cs;
				}
			}
		}

		$foxydb->close();
	 
	   return $result;
	}
 

	function getHotLinksWithinDays($num=30,$withindays=3)
	{
		global $foxydb;

		$foxydb->open();

		$hotlinks = $foxydb->selectHotLinksWithinDays($num,$withindays);
		if($hotlinks){
			foreach($hotlinks as $ln){
				$ret['link'][] = array($ln['sid'],$ln['url'],$ln['name']);
			}
		}

		$foxydb->close();
		return $ret;
	}

	function getRelatedLinksByUrl($purl,$num=30,$pnum=10,$includedesc=0)
	{
		global $foxydb;


		$foxydb->open();

		$link = $foxydb->findLinkByUrl($purl);
		if($link){
			$hottags = $foxydb->selectHotTagsByUrl($purl,$pnum);
			foreach($hottags as $tag){
				$hotlinks = $foxydb->selectHotLinksByTagSid($tag['sid'],$pnum);
				if($links && $hotlinks){
					$links = array_merge($links,$hotlinks);
				}else if (!$links && $hotlinks){
					$links = $hotlinks;
				}
			}

			if($includedesc == 1)
				$ret['link'][] = array($link['sid'],$link['url'],$link['name'],$link['description']);
			else
				$ret['link'][] = array($link['sid'],$link['url'],$link['name']);

			if($links){
				foreach($links as $ln){
					if(!$linksids || $ln['sid']!=$link['sid'] && !in_array($ln['sid'],$linksids)){
						$linksids[] = $ln['sid'];
						if($includedesc == 1)
							$ret['link'][] = array($ln['sid'],$ln['url'],$ln['name'],$ln['description']);
						else
							$ret['link'][] = array($ln['sid'],$ln['url'],$ln['name']);
						$ret['ln2ln'][] = array($link['sid'].$ln['sid'],$link['sid'],$ln['sid'],1);
					}
				}
			}
		}

		$foxydb->close();
		return $ret;
	}
	
 };
 
?>